package threadlocal;

public class ThreadLocalTest {
    public static void main(String[] args) {
        MyRunnable runnable=new MyRunnable();
        new Thread(runnable,"线程1").start();
        new Thread(runnable,"线程2").start();
    }

    public static class MyRunnable implements Runnable{
        ThreadLocal<String> threadLocal1= ThreadLocal.withInitial(() -> "null");
        ThreadLocal threadLocal2= ThreadLocal.withInitial(() -> "null");
        @Override
        public void run() {
            String name = Thread.currentThread().getName();
            threadLocal1.set(name+"的threadLocal1");
            threadLocal2.set(name+"的threadLocal2");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(name + ": "+ threadLocal1.get());
            System.out.println(name + ": "+ threadLocal2.get());
            System.out.println();
        }
    }
}
